import Vue from 'vue'
import App from './App'
import {
	Util
} from "./utils/util";
import store from './store'
/* import VConsole from 'vconsole'

const vConsole = new VConsole() */
Vue.prototype.$store = store
import {
	NetService
} from './common/NetService';
Vue.config.productionTip = false
var wx = require('jweixin-module')

function funcUrlDel(name) {
	var loca = window.location
	var baseUrl = loca.origin + loca.pathname + '?'

	var query = loca.search.substr(1)
	if(query.indexOf(name) > -1) {
		var obj = {}
		var arr = query.split('&')
		for(var i = 0; i < arr.length; i++) {
			arr[i] = arr[i].split('=')
			obj[arr[i][0]] = arr[i][1]
		};
		delete obj[name]
		var url = baseUrl + JSON.stringify(obj).replace(/[\"\{\}]/g, '').replace(/\:/g, '=').replace(/\,/g, '&')
		return url
	} else {
		return window.location.href
	}
}

function share(param) {
	let routes = window.location.href;
	let title = param.title || "渝中城管";
	let desc = param.desc || "网格化系统";
	let img = "http://wggl.cqyz.gov.cn/wechat/images/icon/attr.jpg";
	NetService.wxConfig(routes)
		.then(res => {
			wx.config({
				debug: false,
				appId: res.app_id,
				timestamp: res.timestamp,
				nonceStr: res.nonce_str,
				signature: res.signature,
				jsApiList: [
					'onMenuShareAppMessage', //  分享到朋友接口
					'onMenuShareTimeline', // 分享到朋友圈接口
					'updateTimelineShareData',
					'updateAppMessageShareData'
				]
			});
			wx.checkJsApi({
				jsApiList: [
					// 所有要调用的 API 都要加到这个列表中
					'onMenuShareTimeline', // 分享到朋友圈接口
					'onMenuShareAppMessage', //  分享到朋友接口
					'updateTimelineShareData',
					'updateAppMessageShareData'
				],
				success: function(res) {
					// alert('checkJsApi:success');
				}
			});
			wx.ready(function() {
				//分享给朋友
				wx.updateAppMessageShareData({
					title: title, // 分享标题
					desc: desc, // 分享描述
					link: window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
					imgUrl: img, // 分享图标
					success: function() {
						// alert('好友分享成功');
						// 设置成功
					},
					fail: function(res) {
						// console.log('好友失败', res);
						// alert('好友分享失败');
					},
					cancel: function() {
						// alert('用户取消分享好友');
						// 用户取消分享后执行的回调函数
					}
				});
				//分享到朋友圈
				wx.updateTimelineShareData({
					title: title, // 分享标题
					desc: desc, // 分享描述
					link: window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
					imgUrl: img, // 分享图标
					success: function(res) {
						// 	console.log('朋友圈', res.data);
						// 	// 设置成功
					},
					fail: function(res) {
						// 	console.log('朋友圈失败', res);
						// 	alert('朋友圈分享失败');
					},
					cancel: function() {
						// 	// 用户取消分享后执行的回调函数
					}
				});
			});
			wx.error(function(res) {
				console.log('error', res);
			});
		})
		.catch(err => {});
}

function getOpenId(func) {
	let code = Util.GetQueryString('code')
	var fromurl = funcUrlDel('code')
	//var appid = "wx005fe7ee9b7870eb";
	//var appid = "wx8e71e4fe1acf504a"; //张
	var appid = "wxe7083934d51cb4ef";//线上
	var url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid + '&redirect_uri=' + encodeURIComponent(
		fromurl) + '&response_type=code&scope=snsapi_userinfo&state=redirected#connect_redirect=1#wechat_redirect'
	console.log("opne", uni.getStorageSync('wxtz'))
	/**/
	if(!uni.getStorageSync('wxtz')) {
		window.location.href = url;
		uni.setStorageSync('wxtz', 1)
	} else {
		let oid = uni.getStorageSync('openId');
		if(code && !oid) {
			console.log("1")
			NetService.GetCode(code).then(res => {
				console.log("getCode", res)
				uni.setStorageSync('user', res)
				uni.setStorageSync('openId', res.openid)
				NetService.wxgetuser(res.openid)
					.then(res2 => {
						uni.setStorageSync('user', res2);
						func();
					})
					.catch(err => {
						func();
					});

			}).catch(err => {
				console.log("code系统错误", err)
			});
		} else {
			console.log("2")
			func();
		}
	}
}

Vue.prototype.$getOpenId = getOpenId;
Vue.prototype.$share = share;
App.mpType = 'app'

const app = new Vue({
	...App
})
app.$mount()